﻿using System;
using SocketCore;
using SocketCore.Network;

namespace SocketServerTest
{
    class DummyMessageHandler : INetworkHandler
    {
        private SocketServer sockServer;

        public event EventHandler Connected, Disconnected, MessageReceived, MessageSent, 
            SocketError, SocketIdle;

        public DummyMessageHandler(SocketServer _sock)
        {
            sockServer = _sock;
        }

        #region INetworkHandler Members

        public void OnMessageReceived(IoSession session, Message message)
        {
            sockServer.SocketService.Send(session, message);
            if (MessageReceived != null)
                MessageReceived(session, null);
        }

        public void OnMessageSent(IoSession session, Message message)
        {
            if (MessageSent != null)
                MessageSent(session, null);
        }

        public void OnConnected(IoSession session)
        {
            if(Connected!=null)
            {
                Connected(session, null);
            }
        }

        public void OnDisconnected(IoSession session)
        {
            if (Disconnected != null)
            {
                Disconnected(session, null);
            }
        }

        public void OnError(IoSession session, Exception error)
        {
            if(SocketError != null)
            {
                object[] arr = new object[2] {session, error};
                SocketError(arr, null);
                // if error is.... force close by remote... then call disconnected    
            }
        }

        public void OnIdle(IoSession session)
        {
            if (SocketIdle != null)
                SocketIdle(session, null);
        }

        public INetworkHandler NextNetworkHandler { get; set; }

        #endregion
    }
}
